import { useRequest } from "../../../utils/request";

Component({
    properties: {},
    data: {
        status: 2
    },
    methods: {
        onLoad(options) {
            const { order, price } = options
            this.setData({
                order, price
            })

            // 获取钱包信息
            this.request = new useRequest('api/userwallettotal', {
                data: 'data'
            }, this)

            // 支付
            this.payRequest = new useRequest('api/orderpay', {
                hold: true,
                loading: 'payLoading'
            }, this)
        },

        onUnload() {
            if (!this.data.pay){
                const eventChannel = this.getOpenerEventChannel()
                eventChannel.emit('toDetail')
            }
        },

        changeStatus(e) {
            this.setData({
                status: Number(e.detail.value)
            })
        },

        onPayClick() {
            this.payRequest.run({
                order_no: this.data.order,
                total: this.data.price,
                account_bean: this.data.price,
                pay_way: this.data.status,
            }).then(data => {
                if (this.data.status === 1) {
                    this.pay(data)
                } else {
                    wx.showToast({
                        title: '支付成功',
                        icon: 'none',
                        success: () => {
                            wx.redirectTo({
                                url: '/pages/order/detail/detail?id=' + this.data.order
                            })
                        }
                    })
                }
            })
        },

        pay(data) {
            wx.requestPayment({
                ...data,
                success: () => {
                    this.setData({
                        pay: true
                    })
                    wx.redirectTo({
                        url: '/pages/order/detail/detail?id=' + this.data.order
                    })
                }
            })
        }
    }
});
